package com.xiaoqi.store.service.impl;


import com.xiaoqi.store.dao.AddressMapper;
import com.xiaoqi.store.dao.CateMapper;
import com.xiaoqi.store.dao.OrderMapper;
import com.xiaoqi.store.entity.AddressEntity;
import com.xiaoqi.store.entity.CartVO;
import com.xiaoqi.store.entity.OrderEntity;
import com.xiaoqi.store.entity.OrderItemEntity;
import com.xiaoqi.store.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CateMapper cateMapper;
    @Autowired
    private AddressMapper addressMapper;

    /*
     * @Author xiaoqi
     * @Description  批量插入所有的订单
     * @return
     **/
    @Override
    @Transactional
    public OrderEntity insertAllOrder(Integer aid, Integer[] cids, Integer uid, String uname) {
        // 创建当前时间对象
        Date date = new Date();
        // 根据cids查询所勾选的购物车列表中的数据
        List<CartVO> voByCids = cateMapper.findVOByCids(cids);
        Long allPrice = null;
        // 计算这些商品的总价
        for (CartVO c : voByCids) {
            allPrice += c.getRealPrice();
        }
        log.info("获取商品的总价");
        // 创建订单数据对象
        OrderEntity orderEntity = new OrderEntity();
        // 补全数据：uid
        orderEntity.setUid(uid);
        // 查询收货地址数据
        AddressEntity userAddressByAid = addressMapper.findUserAddressByAid(aid);
        // 补全数据：收货地址相关的6项
        String provinceName = userAddressByAid.getProvinceName();
        orderEntity.setRecv_province(provinceName);
        String cityName = userAddressByAid.getCityName();
        orderEntity.setRecv_city(cityName);
        String areaName = userAddressByAid.getAreaName();
        orderEntity.setRecv_area(areaName);
        String address = userAddressByAid.getAddress();
        orderEntity.setRecv_address(address);
        String phone = userAddressByAid.getPhone();
        orderEntity.setRecv_phone(phone);
        String name = userAddressByAid.getName();
        orderEntity.setRecv_name(name);
        // 补全数据：totalPrice
        orderEntity.setTotal_price(allPrice);
        // 补全数据：status
        orderEntity.setStatus(0);
        // 补全数据：下单时间
        orderEntity.setOrder_time(date);
        // 补全数据：日志
        orderEntity.setCreatedTime(date);
        orderEntity.setCreatedUser(uname);
        orderEntity.setModifiedTime(date);
        orderEntity.setModifiedUser(uname);
        // 插入订单数据
        orderMapper.insertOrder(orderEntity);

        // 遍历carts，循环插入订单商品数据
        for (CartVO c : voByCids) {
            // 创建订单商品数据
            OrderItemEntity orderItemEntity = new OrderItemEntity();
            // 补全数据：oid(order.getOid())
            orderItemEntity.setOid(orderEntity.getOid());
            // 补全数据：pid, title, image, price, num
            orderItemEntity.setPid(c.getPid());
            orderItemEntity.setTitle(c.getTitle());
            orderItemEntity.setImage(c.getImg());
            orderItemEntity.setPrice(c.getRealPrice());
            orderItemEntity.setNum(c.getNum());

            // 补全数据：4项日志
            orderEntity.setCreatedTime(date);
            orderEntity.setCreatedUser(uname);
            orderEntity.setModifiedTime(date);
            orderEntity.setModifiedUser(uname);
            // 插入订单商品数据
            orderMapper.insertOrderItem(orderItemEntity);
        }

        // 返回
        return orderEntity;
    }
}
